//---------------------------------------------------------------------
// File: BAMDeploymentStep.cs
// 
// Summary: 
//
//---------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

namespace Microsoft.Services.BizTalkApplicationFramework.BizUnit
{
	using System;
	using System.Diagnostics;
	using Microsoft.Services.BizTalkApplicationFramework.BizUnit;

	/// <summary>
	/// The BAMDeploymentStep deloys a BAM definition.
	/// </summary>
	/// 
	/// <remarks>
	/// The following shows an example of the Xml representation of this test step.
	/// 
	/// <code escaped="true">
    /// <TestStep assemblyPath="" typeName="Microsoft.Services.BizTalkApplicationFramework.BizUnit.BAMDeployment, Microsoft.Services.BizTalkApplicationFramework.BizUnit.BizTalkSteps">
	///		<DelayForCompletion>5</DelayForCompletion> <!-- Optional, seconds to delay for this step to complete -->
	///		<TrackingFolderPath>C:\Program Files\Microsoft BizTalk Server 2004\Tracking</TrackingFolderPath>
	///		<BAMDefinitionXmlFilePath>C:\BizUnitTest\Solution\View All.xml</BAMDefinitionXmlFilePath>
	///		<Action>Deploy</Action>
	/// </TestStep>
	/// </code>
	///
	///	<list type="table">
	///		<listheader>
	///			<term>Tag</term>
	///			<description>Description</description>
	///		</listheader>
	///		<item>
	///			<term>DelayForCompletion</term>
	///			<description>The delay for this step to complete in seconds <para>(optional)</para></description>
	///		</item>
	///		<item>
	///			<term>TrackingFolderPath</term>
	///			<description>The Tracking directory for BizTalk Server</description>
	///		</item>
	///		<item>
	///			<term>BAMDefinitionXmlFilePath</term>
	///			<description>The path to the BAM definition file</description>
	///		</item>
	///		<item>
	///			<term>Action</term>
	///			<description>Deploy|Undeploy</description>
	///		</item>
	///	</list>
	///	</remarks>

	public class BAMDeploymentStep : ITestStep
	{
		internal enum BAMDeploymentAction 
		{
			Deploy,
			Undeploy
		}

		/// <summary>
		/// ITestStep.Execute() implementation
		/// </summary>
		/// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
		/// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
		public void Execute(System.Xml.XmlNode testConfig, Context context)
		{
			string workingFolder = context.ReadConfigAsString(testConfig, "TrackingFolderPath");
			string bamDefinitionFile = context.ReadConfigAsString(testConfig, "BAMDefinitionXmlFilePath");
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			sb.Append('"');
			sb.Append(bamDefinitionFile);
			sb.Append('"');
			
			Process bamProcess = new Process();
			bamProcess.StartInfo.WorkingDirectory = workingFolder;
			bamProcess.StartInfo.FileName = "bm.exe";

			BAMDeploymentAction action = (BAMDeploymentAction)Enum.Parse( typeof(BAMDeploymentAction), testConfig.SelectSingleNode("Action").InnerText, true );

			if ( action == BAMDeploymentAction.Deploy ) 
			{
				context.LogInfo("Deploying BAM definition file {0}", bamDefinitionFile );
				bamProcess.StartInfo.Arguments = "deploy " + sb.ToString();
			}
			else 
			{
				context.LogInfo("Undeploying BAM definition file {0}", bamDefinitionFile );
				bamProcess.StartInfo.Arguments = "undeploy all " + sb.ToString();
			}

			bamProcess.StartInfo.UseShellExecute = true;

			bamProcess.Start();

			// Delay if necessary to allow the orchestration to start/stop
			if (testConfig.SelectSingleNode("DelayForCompletion") != null)
			{
				int delayForCompletion = Convert.ToInt32(testConfig.SelectSingleNode("DelayForCompletion").InnerText);
				context.LogInfo("Waiting for {0} seconds before recommencing testing.", delayForCompletion);
				System.Threading.Thread.Sleep(delayForCompletion*1000);
			}
		}
	}
}
